(function ($) {
    "use strict";


    /*==================================================================
   [ Focus input ]*/
    $('.input100').each(function () {
        $(this).on('blur', function () {
            if ($(this).val().trim() != "") {
                $(this).addClass('has-val');
            } else {
                $(this).removeClass('has-val');
            }
        })
    })


    /*==================================================================
    [ Validate ]*/
    var input = $('.validate-input .input100');

    $('.validate-form').on('submit', function () {
        var check = true;

        for (var i = 0; i < input.length; i++) {
            if (validate(input[i]) == false) {
                showValidate(input[i]);
                check = false;
            }
        }

        if (check) {
            requestAuthorizeLogin().then(
                (res) => {
                    console.log(res)
                    if (res.code != 200) {
                        alert(res.msg)
                        return
                    }
                    // console.log(res);
                    var data = res.data;
                    var dataset = {
                        uid: data.userInfo.upid,
                        groupId: data.userInfo.upid,
                        nickname: data.userInfo.zhicheng,
                        portrait: data.userInfo.pic,
                        token: data.authKey,
                        retrospect_model: data.retrospect_model,
                        UserInfo: data.userInfo
                    };
                    console.log(dataset);

                    window.sessionStorage.setItem("groupId", data.userInfo.groupid);
                    window.localStorage.setItem("menus", JSON.stringify(data.menu));
                    window.sessionStorage.setItem("user", JSON.stringify(dataset));
                    // window.location.reload();
                    // eixt;

                    window.location.href = "/";
                    // window.location.href = "/admin/index.html";


                }
            ).catch(frs => {
                alert("登陆失败");
                console.log(frs);
                alert(frs.data.msg);
            });
        }

        return false;
    });


    $('.validate-form .input100').each(function () {
        $(this).focus(function () {
            hideValidate(this);
        });
    });

    function validate(input) {
        if ($(input).attr('type') == 'email' || $(input).attr('name') == 'email') {
            if ($(input).val().trim().match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{1,5}|[0-9]{1,3})(\]?)$/) == null) {
                return false;
            }
        } else {
            if ($(input).val().trim() == '') {
                return false;
            }
        }
    }

    function showValidate(input) {
        var thisAlert = $(input).parent();

        $(thisAlert).addClass('alert-validate');
    }

    function hideValidate(input) {
        var thisAlert = $(input).parent();

        $(thisAlert).removeClass('alert-validate');
    }
})(jQuery);

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function qsx(data) {
    var arr = [];
    for (var name in data) {
        arr.push(
            encodeURIComponent(name) + "=" + encodeURIComponent(data[name])
        );
    }
    arr.push(("v=" + Math.random()).replace(".", ""));
    return arr.join("&");
}

function requestAuthorizeLogin() {
    return new Promise(function (resolve, reject) {
        axios.defaults.timeout = 60000;
        axios.defaults.baseURL = "";

        var url = "/api/admin/login",
            params = {
                name: document.getElementById("account").value,
                // password: hex_md5(document.getElementById("password").value)
                pwd: document.getElementById("password").value,
                openid: "shadow"
            };

        axios.post(url, window.Qs.stringify(params)).then(response => {
            if (response.status == 200) {

                    resolve(response.data);

            } else {
                reject(response);
            }
        }).catch(frs => {
            reject(frs);
        });
    });
}

window.onload = function () {
    var user = window.sessionStorage.getItem("user") || false;
    if (user && user != "undefined") {
        window.location.href = "/admin/index.html";
    }
}
